<?php

class nbwiException extends Exception
{
	const STARTUP_ERROR		= 1;
	const DATABASE_ERROR	= 2;
	const EXT_ERROR			= 3;
	
	private function outputHtml(array $traces = array())
	{
		$doc = new DOMDocument();
		$doc->formatOutput = true;
		
		if (!headers_sent())
		{
			$html	= $doc->createElement('html');
			$head	= $doc->createElement('head');
			$title	= $doc->createElement('title', 'nbWi Exception');
			
			$head->appendChild($title);
			
			$body = $doc->createElement('body');
			$body->setAttribute('style', 'text-align: center');
		}
		
		$style = 'background-color: #ffcccc;
			border: 1px dashed #990000;
			color: #990000;
			margin: 0 auto;
			padding: 10px;
			text-align: left;
			width: 50%';
		
		$div = $doc->createElement('div');
		$div->setAttribute('style', $style);
		
			$span1 = $doc->createElement('span', 'Nbwi Error: ');
			$span1->setAttribute('style', 'font-weight: bold');
			
			$span2 = $doc->createElement('span', $this->getMessage());
		
			$div2 = $doc->createElement('div');
			$div2->setAttribute('style', 'margin-top: 10px');
			
				$span3 = $doc->createElement('span', 'Stack Trace:');
				$span3->setAttribute('style', 'font-weight: bold');
			
			$div2->appendChild($span3);
			
			foreach ($traces as $trace)
			{
				$divTrace = $doc->createElement('div', $trace);
				$divTrace->setAttribute('style', 'margin-top: 10px');
				$div2->appendChild($divTrace);
			}
		
		$div->appendChild($span1);
		$div->appendChild($span2);
		$div->appendChild($div2);
		
		if (!headers_sent())
		{
			$body->appendChild($div);
			
			$html->appendChild($head);
			$html->appendChild($body);
			
			$doc->appendChild($html);
		}
		else
		{
			$doc->appendChild($div);
		}
		
		return $doc->saveHTML();
	}
	
	public function trigger()
	{
		$traces	= array();
		$count	= 0;
		
		foreach ($this->getTrace() as $trace)
		{
			$string = '#' . $count . ' ';
			$string .= $trace['file'] . '(' . $trace['line'] . ')';
			
			$args = implode(', ', $trace['args']);
			
			if (isset($trace['class']) && $trace['class'] != '')
			{
				$string .= ': ' . $trace['class'] . $trace['type'] . $trace['function'] . '(' . $args . ')';
			}
			elseif ($trace['function'] != '')
			{
				$string .= ': ' . $trace['function'] . '(' . $args . ')';
			}
			
			$traces[$count] = $string;
			
			++$count;
		}
		
		return $this->outputHtml($traces);
	}
}

?>